﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using JXC.Core.Models;
using JXC.EFCore;
using JXC.EFCore.Helper;
using JXC.Application.Dtos;
using AutoMapper;

namespace JXC.WebAPI.Controllers
{
    /// <summary>
    /// 采购入库
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class CaigouEntersController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly IMapper _mapper;

        public CaigouEntersController(
            AppDbContext context,
            IMapper mapper
            )
        {
            _context = context;
            _mapper = mapper;

        }

        // GET: api/CaigouEnters
        [HttpGet]
        public async Task<ActionResult<IEnumerable<CaigouEnter>>> GetCaigouEnter()
        {
            IQueryable<CaigouEnter> caigouEnters = _context.CaigouEnter.Include(p => p.CaigouStorageDetails);
            var res = await caigouEnters.ToListAsync();
            return Ok(DataResult(res,"获取采购入库列表成功")); 
        }

        // GET: api/CaigouEnters/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CaigouEnter>> GetCaigouEnter(Guid id)
        {
            var caigouEnter = await _context.CaigouEnter.FindAsync(id);

            if (caigouEnter == null)
            {
                return NotFound();
            }

            return caigouEnter;
        }

        // PUT: api/CaigouEnters/5
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPut("{id}")]
        public async Task<IActionResult> PutCaigouEnter(Guid id, CaigouEnter caigouEnter)
        {
            if (id != caigouEnter.Id)
            {
                return BadRequest();
            }

            _context.Entry(caigouEnter).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CaigouEnterExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/CaigouEnters
        // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
        [HttpPost]
        public async Task<ActionResult<CaigouEnter>> PostCaigouEnter(CaigouEnterDto caigouEnterDto)
        {
            CaigouEnter caigouEnter = _mapper.Map<CaigouEnter>(caigouEnterDto);
            await _context.CaigouEnter.AddAsync(caigouEnter);
            foreach (var item in caigouEnterDto.CaigouStorageDetailsDtos)
            {
                item.CaigouEnterId = caigouEnter.Id;
            }
            List<CaigouStorageDetails> caigouStorageDetails = _mapper.Map<List<CaigouStorageDetails>>(caigouEnterDto.CaigouStorageDetailsDtos);
            await _context.AddRangeAsync(caigouStorageDetails);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetCaigouEnter", new { id = caigouEnter.Id }, DataResult(caigouEnter));

        }

        // DELETE: api/CaigouEnters/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteCaigouEnter(Guid id)
        {
            var caigouEnter = await _context.CaigouEnter.FindAsync(id);
            if (caigouEnter == null)
            {
                return NotFound();
            }

            _context.CaigouEnter.Remove(caigouEnter);
            await _context.SaveChangesAsync();

            return NoContent();
        }

        private bool CaigouEnterExists(Guid id)
        {
            return _context.CaigouEnter.Any(e => e.Id == id);
        }
    }
}
